#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

#define N 30

int ans = 0x7fffffff;
int nn;
int s[N], b[N]; //s酸，b苦

void dfs(int n, int x, int y){ //第n种，总酸，总苦
    if(n > nn){
        if(x == 1 && y == 0)    return; //啥也没有
        ans = min(ans, abs(x - y));
        return;
    }
    dfs(n + 1, x*s[n], y + b[n]); //要么加
    dfs(n + 1, x, y);   //要么不加
}

int main(){
    cin >> nn;
    for(int i = 1; i <= nn; i++){
        cin >> s[i] >> b[i];
    }
    dfs(1, 1, 0);
    cout << ans;
    return 0;
}